﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMU.Certificates;

namespace CMU.Import
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        CertificateEntities context = new CertificateEntities();
        
        private void MainWindow_Load(object sender, EventArgs e)
        {
            Import imp = new Import();
            DataTable combo = imp.getComboData();
            cmb_Chonlop.DataSource = combo;
            cmb_Chonlop.DisplayMember = "value";
            cmb_Chonlop.ValueMember = "ID";
        }
        string filename = "";
        private void btn_Browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Mở file";
            open.Filter = "Excel files|*.xls;*.xlsx|All file types|*.*";
            if (open.ShowDialog() == DialogResult.OK)
            {
                txt_Browse.Text = open.FileName.ToString();
                filename = open.SafeFileName.ToString();
            }
        }

        private void btn_Import_Click(object sender, EventArgs e)
        {
            Import imp = new Import();
            //Layout du lieu tu database gan vao table
            DataTable input = imp.getComboData();
            //lay ID cua combo box lam du kien de importthong tin vao bang sinhvien va chungchi
            int ID = Convert.ToInt32(cmb_Chonlop.SelectedValue);
            DataRow[] dr = new DataRow[10];
            dr = input.Select("ID=" + ID.ToString());
            List<string> sameID = new List<string>();
            if (dr.Count() > 0)
            {
                switch (imp.ImportFromExcel(Convert.ToInt32(dr[0][1]), txt_Browse.Text.ToString().Trim(), ref sameID))
                {
                    case 0:
                        MessageBox.Show("Excel không có dữ liệu để import", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt_Browse.Text = "";
                        break;
                    case 1:
                        string trung = dr[0][2].ToString();
                        MessageBox.Show("Đã import thành công dữ liệu từ file " + filename + " vào môn học " + trung, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt_Browse.Text = "";
                        break;
                    case 2:
                        string trung2 = dr[0][2].ToString();
                        string list = "Môn học " + trung2 + " đã tồn tại chứng chỉ của các sinh viên sau: " + sameID[0].ToString();
                        for (int i = 1; i < sameID.Count; i++)
                        {
                            list = list + ", " + sameID[i].ToString();
                        }
                        MessageBox.Show(list, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txt_Browse.Text = "";
                        break;
                }
            }
        }
    }
}